<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>添加收货地址</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <style>
    header {
        position: relative;
        width: 100%;
        height: 50px;
        background-color: #e3007f
    }
    
    header .back {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 80px;
        height: 50px;
        background: url(../image/back.png);
        background-position: 12px 16px;
        background-size: 11px 18px;
        background-repeat: no-repeat;
    }
    
    header h1 {
        width: 100%;
        height: 50px;
        line-height: 50px;
        text-align: center;
        color: #fff;
        font-size: 20px;
    }
    
    header .right {
        display: none;
        position: absolute;
        bottom: 0;
        right: 0;
        width: 50px;
        height: 50px;
        line-height: 50px;
        color: #fff;
        font-size: 15px;
        text-align: center;
    }
    </style>
</head>

<body>
    <header id="header">
        <div class="back" tapmode onclick="api.closeWin();"></div>
        <h1>收货地址</h1>
        <div id="right" class="right" tapmode onclick="fnDelete();">删除</div>
    </header>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
apiready = function() {
    if (api.pageParam.id) {
        var right = $api.byId('right');
        right.style.display = 'block';
    }

    var header = $api.byId('header');
    $api.fixStatusBar(header);
    var headerH = $api.offset(header).h;
    api.openFrame({
        name: 'setaddress_frame',
        url: './setaddress_frame.html',
        rect: {
            x: 0,
            y: headerH,
            w: 'auto',
            h: 'auto'
        },
        pageParam: api.pageParam,
        bounces: false
    });
};

function fnDelete() {
    var userInfo = $api.getStorage('userInfo');
    api.confirm({
        title: '提示',
        msg: '删除收货地址',
        buttons: ['确定', '取消']
    }, function(ret, err) {
        if (ret) {
            if (1 == ret.buttonIndex) {
                api.ajax({
                    url: 'https://d.apicloud.com/mcm/api/address/' + api.pageParam.id,
                    method: 'delete',
                    headers: {
                        "X-APICloud-AppId": "A6921544633372",
                        "X-APICloud-AppKey": "2672b5911d8551540c1ea598e01c87fee217a1e5.1482500122476",
                        "Authorization": userInfo.id
                    }
                }, function(ret, err) {
                    if (ret) {
                        api.sendEvent({
                            name: 'updateAddress'
                        });
                        api.closeWin();
                    } else {
                        alert(JSON.stringify(err));
                    }
                });
            }
        }
    });
}
</script>

</html>
